package com.xjh.him.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.google.android.material.appbar.AppBarLayout;
import com.xjh.him.R;
import com.xjh.him.adapter.TrendAdapter;
import com.xjh.him.common.Constant;
import com.xjh.him.core.BaseActivity;
import com.xjh.him.databinding.ActivityTrendBinding;
import com.xjh.him.dto.trend.TrendDTO;
import com.xjh.him.dto.user.UserDTO;
import com.xjh.him.utils.MMKVUtils;
import com.xjh.him.utils.Utils;
import com.xjh.him.utils.XToastUtils;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;

import org.xjh.tool.Checks;
import org.xjh.tool.vo.PageVO;

import java.util.List;


public class TrendActivity extends BaseActivity<ActivityTrendBinding> {
    public static final int PAGE_CODE = 194827;

    private UserDTO userDTO;
    private static TrendAdapter trendAdapter;
    private int page = 1;
    public static Handler handler;

    @Override
    protected ActivityTrendBinding viewBindingInflate(LayoutInflater inflater) {
        return ActivityTrendBinding.inflate(inflater);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
        super.onCreate(savedInstanceState);

        handler = new Handler();
        String json = (String) MMKVUtils.get(Constant.MMKV_KEY_USER_DTO, "");
        if (!Checks.isEmpty(json)) {
            userDTO = JSON.parseObject(json, UserDTO.class);
        }

        initViews();
        initListeners();
    }

    private void initListeners() {
        binding.appBar.addOnOffsetChangedListener(new AppBarLayout.BaseOnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                    binding.ctlToolbarLayout.setTitle("朋友圈");
                    binding.ivAvatar.setVisibility(View.GONE);
                }else {
                    binding.ctlToolbarLayout.setTitle("");
                    binding.ivAvatar.setVisibility(View.VISIBLE);
                }
            }
        });
        binding.btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popPage();
            }
        });
        binding.btnCamera.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(TrendActivity.this, TrendPublishActivity.class);
            startActivityForResult(intent, PAGE_CODE);
        });
    }

    private void initViews() {
        WidgetUtils.initRecyclerView(binding.rvList, 0);

        binding.tvNickname.setText(userDTO.getNickname());
        Glide.with(this)
                .load(userDTO.getAvatarUrl())
                .apply(Utils.getRoundedCornersRequestOptions(5))
                .into(binding.ivAvatar);

        String listTrendUrl = Utils.getWebServerApiUrl(this, getString(R.string.api_trend_list));
        PageVO pageVO = new PageVO();
        SimpleCallBack<List<TrendDTO>> listTrendCB = new SimpleCallBack<List<TrendDTO>>() {
            @Override
            public void onSuccess(List<TrendDTO> trendDTOList) throws Throwable {
                trendAdapter = new TrendAdapter(TrendActivity.this, trendDTOList);
                binding.rvList.setAdapter(trendAdapter);
            }

            @Override
            public void onError(ApiException e) {
                XToastUtils.toast(e.getDisplayMessage());
            }
        };
        Utils.doPost(listTrendUrl, JSON.toJSONString(pageVO), listTrendCB);
    }

    public static void refreshTrendList(Activity activity) {
        String listTrendUrl = Utils.getWebServerApiUrl(activity, activity.getString(R.string.api_trend_list));
        PageVO pageVO = new PageVO();
        SimpleCallBack<List<TrendDTO>> listTrendCB = new SimpleCallBack<List<TrendDTO>>() {
            @Override
            public void onSuccess(List<TrendDTO> trendDTOList) throws Throwable {
                trendAdapter.setTrendDTOList(trendDTOList);
                trendAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(ApiException e) {
                XToastUtils.toast(e.getDisplayMessage());
            }
        };
        Utils.doPost(listTrendUrl, JSON.toJSONString(pageVO), listTrendCB);
    }
}
